//object 2 query
/*
  //传入
  data = {
    query: {
      id: 1,
      name: "天马图文"
    },
    page: {
      limit: 10,
      offset: 0,
    }
  };
  
  // object TO queryString ↓

  //输出
  query={"user_id":"11", "created_at":1538218252}&page={"limit":10,"offset":0}
*/
export function object2Query(data) {
    let str = [];
    for (let key in data) {
        let strKey = `${key}={`;
        let query = [];
        for (let i in data[key]) {
            //number 类型不用带双引号
            if (typeof data[key][i] == "number") {
                query.push(`"${i}":${data[key][i]}`)
            } else {
                query.push(`"${i}":"${data[key][i]}"`)
            }

        }
        query = query.join(",");
        strKey += `${query}}`;
        str.push(strKey);
    }
    str = str.join("&");
    return str;
}

export function timetrans(timetamps) {
    let date = new Date(timetamps * 1000) //10位时间戳需要乘1000
    let dateObj = {};
    let Y = date.getFullYear() + "";
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1 + "");
    let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate() + "");
    let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours() + "");
    let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() + "");
    let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds() + "";
    dateObj = {
        Y,
        M,
        D,
        h,
        m,
        s,
        date: `${Y}-${M}-${D}`,
        dateTime: `${Y}-${M}-${D} ${h}:${m}:${s}`,
        dateTime_: `${Y}-${M}-${D} ${h}:${m}`,
        time: `${h}:${m}:${s}`,
        time_: `${h}:${m}`,
    };
    return dateObj;
}
